<?php
include('insert_advert.php');
/**
 * Created by PhpStorm.
 * User: tsa
 * Date: 17/12/2013
 * Time: 11:34 πμ
 */

if ($_POST['click'] == "1")
{

    $click = "0";
    $_POST["click"] = "0";
    $sql_q = 'SELECT * FROM `menu` WHERE `parent_id` = '.intval($_POST['id']);
    $result = mysqli_query($con, $sql_q);
    $row = mysqli_num_rows($result);
    if ($row == 0)
    {
        $sql_p = 'SELECT * FROM `field` WHERE `menu_id` = '.intval($_POST['id']);
        //$sql_q = '" INSERT INTO `field` (`id`,`name`,`divname`,`parent_id`) VALUES (NULL,\''.$_POST['name'].'\',\''.$_POST['divname'].'\','.intval($_POST['parent_id']).') "';
        $result_p = mysqli_query($con, $sql_p);

        $form = '<form id="form_insert_adverb" method="post">';
        while($row_p = mysqli_fetch_array($result_p))
        {
            $sql_c = 'SELECT * FROM `field` WHERE `parent_id` = '.$row_p['id'];
            $result_c = mysqli_query($con, $sql_c);
            $row_c = mysqli_num_rows($result_c);
        if ($row_c == 0)
        {
            $form = $form . '<label>'.$row_p['name'].'</label>';
            $form = $form . '<input name="'.$row_p['divname'].'" type="text" value=""/>';
        }
        else
        {
        $form = $form . '<label>'.$row_p['name'].'</label>';
        $form = $form . '<select name="id"/>';
        $form = $form . '<option value="0" selected="selected"</option>';
        while($row_d = mysqli_fetch_array($result_c))
        {
            $form = $form . '<option value="'.$row_d['id'].'">'.$row_d['name'].'</option>';
        }
        $form = $form . '</select>';
        }
        }
        $form = $form . '<input name="click" type="hidden" value="1"/>';
        $form = $form . '<input type="submit" value="Go"/>';
        $form = $form . '</form>';
    }
    else
    {
        $form = '<form id="form_insert_adverb" method="post">';
        $form = $form . '<label>Επιλέξτε</label>';
        $form = $form . '<select name="id"/>';
        $form = $form . '<option value="0" selected="selected"</option>';
        while($row = mysqli_fetch_array($result))
        {
            $form = $form . '<option value="'.$row['id'].'">'.$row['name'].'</option>';
        }
        $form = $form . '</select>';
        $form = $form . '<input name="click" type="hidden" value="1"/>';
        $form = $form . '<input type="submit" value="Go"/>';
        $form = $form . '</form>';
    }
    echo $sql_q;
}
else
{
    $click = "0";
    $_POST["click"] = "0";
    $sql_q = 'SELECT * FROM `menu` WHERE `parent_id` = 0';
    $result = mysqli_query($con, $sql_q);
    $form = '<form id="form_insert_adverb" method="post">';
    $form = $form . '<label>Επιλέξτε</label>';
    $form = $form . '<select name="id"/>';
    $form = $form . '<option value="0" selected="selected"</option>';
    while($row = mysqli_fetch_array($result))
    {
        $form = $form . '<option value="'.$row['id'].'">'.$row['name'].'</option>';
    }
    $form = $form . '</select>';
    $form = $form . '<input name="click" type="hidden" value="1"/>';
    $form = $form . '<input type="submit" value="Go"/>';
    $form = $form . '</form>';
}
echo $form;
?>